#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=icons\script.ico
#AutoIt3Wrapper_Outfile=..\cv5-starter.exe
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_Icon_Add=icons\catiads2_vista.ico
#AutoIt3Wrapper_Res_Icon_Add=icons\ds.ico
#AutoIt3Wrapper_Res_Icon_Add=icons\run.ico
#AutoIt3Wrapper_Res_Icon_Add=icons\script.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiComboBox.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("cv5-starter", 260, 99, 192, 124)
;~ GUISetIcon(@ScriptDir & "\icon.ico", -1)
$BtnStart = GUICtrlCreateButton("Start", 176, 64, 73, 25)
$hCombo = GUICtrlCreateCombo("", 8, 32, 241, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
$Label1 = GUICtrlCreateLabel("Select the application to start", 8, 8, 141, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $var

; Try to read config.ini
$var = IniReadSection(@ScriptDir & "\config.ini", "LIST")
If @error Then
	MsgBox(4096, "", "Error occurred, probably no INI file.")
	Exit 1
EndIf
; Loop through list inserting elements into combo
For $i = 1 To $var[0][0]
	; MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
	GUICtrlSetData($hCombo, $var[$i][0])
Next
; Set the top element into the combo edit box
_GUICtrlComboBox_SetEditText($hCombo, $var[1][0])

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $BtnStart
			ConsoleWrite("BtnStart" & @CRLF)
			_StartCmd(GUICtrlRead($hCombo))
	EndSwitch
WEnd

Func _StartCmd($AppName)
	ConsoleWrite("_StartCmd: " & $AppName & @CRLF)
	Local $cmd = IniRead(@ScriptDir & "\config.ini", "LIST", $AppName, "")
	If $cmd = "" Then
		MsgBox(4096, "", "This application doesn't exist or is not properly define.")
	Else
		EnvSet("ScriptDir", @ScriptDir)
		$cmd = _GetPathByEnv($cmd)
		ConsoleWrite("Command-line: " & $cmd & @CRLF)
		Run($cmd, "")
	EndIf
EndFunc

Func _GetPathByEnv($Path)
    Local $ExpandEnvStrings = Opt("ExpandEnvStrings", 1)
    Local $Return = $Path
    Opt("ExpandEnvStrings", $ExpandEnvStrings)
    Return $Return
EndFunc